package com.nf.health.app.customview;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.adapter.OnMyClickListener;

/**
 * Created by hepan on 2016/11/16.
 * 干预详情的popupWindow
 */
public class SportRecipePopupWindow extends PopupWindow implements View.OnClickListener {

    private ListView lvMenu;
    private TextView tvHistory;

    private View rootView;
    private Context ctx;
    private String[] menus = new String[]{"跑步机心肺耐力测试报告", "心藏功能检测报告", "汗液分析检测报告"};
    private OnMyClickListener listener;

    public SportRecipePopupWindow(Activity act) {
        super(act);
        ctx = act;

        rootView = LayoutInflater.from(act).inflate(R.layout.pop_more_sport_recipe, null);

        this.setContentView(rootView);
        this.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.setAnimationStyle(android.R.style.Animation_Dialog);
        this.setBackgroundDrawable(new BitmapDrawable());//必要,保证背景是自定义的.9patch图
        initView();
        initListener();
    }

    private void initView() {

        lvMenu = (ListView) rootView.findViewById(R.id.lv_menu);

        tvHistory = (TextView) rootView.findViewById(R.id.tv_history);

        lvMenu.setAdapter(new MenuAdapter(menus));

    }

    private void initListener() {
        lvMenu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (listener != null) {
                    listener.onClick(position, "0");//0代表menus
                }
                dismiss();
            }
        });
        tvHistory.setOnClickListener(this);
    }

    public void setOnMyItemClickListener(OnMyClickListener listener) {
        this.listener = listener;
    }

    @Override
    public void onClick(View v) {
        this.dismiss();
        if (listener != null) {
            listener.onClick(v.getId(), "1");//1代表历史方案,购买更多
        }
    }


    /**
     * 执行方案中plans适配器 数据写死的
     */
    private class MenuAdapter extends BaseAdapter {
        private String[] menus;

        public MenuAdapter(String[] menus) {
            this.menus = menus;
        }

        @Override
        public int getCount() {
            return menus.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            MenuAdapterViewHolder holder;
            if (convertView == null) {
                convertView = View.inflate(ctx, R.layout.simple_textview, null);
                holder = new MenuAdapterViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (MenuAdapterViewHolder) convertView.getTag();
            }
            holder.text1.setText(menus[position]);
            return convertView;
        }
    }

    private class MenuAdapterViewHolder {
        TextView text1;

        MenuAdapterViewHolder(View view) {
            text1 = (TextView) view.findViewById(R.id.text1);
        }
    }

    private class MyListAdapterViewHolder {
        TextView text1;

        MyListAdapterViewHolder(View view) {
            text1 = (TextView) view.findViewById(R.id.text1);
        }
    }

}
